package com.atguigu.edu.controller.front;

import com.atguigu.commonUtils.JwtUtils;
import com.atguigu.commonUtils.R;
import com.atguigu.edu.entity.Comment;
import com.atguigu.edu.entity.vo.MemberVo;
import com.atguigu.edu.feign.UcenterClient;
import com.atguigu.edu.service.CommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/edu/commentFront")
//@CrossOrigin //gateway配置全局跨域，就不要再加跨域注解
public class CommentFrontController {

    @Autowired
    private UcenterClient ucenterClient;

    @Autowired
    private CommentService commentService;

    /**
     * 根据token获取用户的 信息
     *
     * @param request
     * @return
     */
    @GetMapping("getMemberInfo")
    public MemberVo getMemberInfo(HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        MemberVo memberInfo = ucenterClient.getMemberInfoById(memberId);

        return memberInfo;
    }

    /**
     * 根据课程ID 分页查询课程的评论信息
     *
     * @param page
     * @param limit
     * @param courseId
     * @return
     */
    @GetMapping("getComment/{page}/{limit}/{courseId}")
    public R index(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page, @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit, @ApiParam(name = "courseId", value = "课程ID", required = true) @PathVariable String courseId) {
        Page<Comment> pageParam = new Page<>(page, limit);

        commentService.page(pageParam, new QueryWrapper<Comment>().eq("course_id", courseId).orderByDesc("gmt_create"));

        List<Comment> commentList = pageParam.getRecords();

        HashMap<String, Object> map = new HashMap<>();
        map.put("items", commentList);
        map.put("current", pageParam.getCurrent());
        map.put("pages", pageParam.getPages());
        map.put("size", pageParam.getSize());
        map.put("total", pageParam.getTotal());
        map.put("hasNext", pageParam.hasNext());
        map.put("hasPrevious", pageParam.hasPrevious());

        return R.ok().data(map);
    }

    /**
     * 添加课程评论
     *
     * @param comment
     * @param request
     * @return
     */
    @PostMapping("addComment")
    public R addComment(@RequestBody Comment comment, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        if (StringUtils.isEmpty(memberId)) {
            return R.error().code(20004).message("请先进行登录！");
        }

        comment.setMemberId(memberId);
        MemberVo memberInfo = ucenterClient.getMemberInfoById(memberId);
        comment.setNickname(memberInfo.getNickname());
        comment.setAvatar(memberInfo.getAvatar());

        commentService.save(comment);
        return R.ok();
    }
}
